package com.sias.Loli.service.impl;

import com.sias.Loli.dao.LeaderDao;
import com.sias.Loli.entity.Leader;
import com.sias.Loli.entity.LeaderLog;
import com.sias.Loli.service.intf.LeaderLogService;
import com.sias.Loli.service.intf.LeaderService;
import com.sias.Loli.util.Rutil;
import com.sias.Loli.vo.PageBean;
import com.sias.Loli.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
@Service
public class LeaderServiceImpl implements LeaderService {
    @Autowired
    private LeaderDao leaderDao;
    @Autowired
    private LeaderLogService logService;

    /*
     * 添加
     */
    @Override
    public R insertLeader(HttpSession session, Leader leader) {
        Leader user = (Leader)session.getAttribute("user");
        int i = leaderDao.insertLeader(leader);
        if (i>0){
            logService.insertLeaderLog(new LeaderLog(user.getId(),1,"添加一个用户的操作"));
            return Rutil.ok();
        }
        logService.insertLeaderLog(new LeaderLog(user.getId(),3,"添加操作没有成功"));
        return Rutil.fail();
    }
    /*
    查询
     */

    @Override
    public PageBean selectLeader(Leader leader) {
        List<Leader> leaders = leaderDao.selectLeader(leader);
        return new PageBean(0,"",leaders.size(),leaders);
    }
    /*
    登录验证
     */
    @Override
    public R selectLeadervf(HttpSession session,Leader leader,String codes) {
        if (codes.equalsIgnoreCase(session.getAttribute("code").toString())){
            List<Leader> leaders = leaderDao.selectLeader(leader);
            if (leaders.size()>0){
                session.setAttribute("user",leaders.get(0));
                System.err.println(leaders.get(0).getId());
                logService.insertLeaderLog(new LeaderLog(leaders.get(0).getId(),2,"登录成功"));
                return Rutil.ok();
            }
            logService.insertLeaderLog(new LeaderLog(leaders.get(0).getId(),1,"有人尝试恶意登录并且失败"));
        }

        return Rutil.fail();
    }
}

